package com.jinsi.service.ylt.iml;

import com.jinsi.mapper.ylt.CouponMapper;
import com.jinsi.pojo.SyUserCouponUsed;
import com.jinsi.service.ylt.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CouponServiceImpl
 * @Content 优惠券接口
 * @Author 姚龙腾
 * @Date 2022/10/12 10:40
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;


    /**
     * 查询有的优惠券(模糊查询所需的信息)
     * @param discountsName
     * @param discountsType
     * @param discountsQuantity
     * @return List<SyUserCouponUsed>
     */
    @Override
    @Transactional(readOnly = true)
    public List<SyUserCouponUsed> getAll(
            String discountsName,
            String discountsType,
            Integer discountsQuantity) {
        return couponMapper.getAll(discountsName,discountsType,discountsQuantity);
    }


    /**
     * 添加的优惠券
     * @param coupon
     * @throws Exception
     */
    @Override
    public void addCoupon(SyUserCouponUsed coupon) throws Exception {
        int num = couponMapper.addCoupon(coupon);
        if (num == 0){
            throw new Exception();
        }
    }


    /**
     * 修改上架还是下架
     * @param discountsId
     * @param discountsQuantity
     * @throws Exception
     */
    @Override
    public void upCouponState(int discountsId, int discountsQuantity) throws Exception {
        int num = couponMapper.upCouponState(discountsId,discountsQuantity);
        if (num == 0){
            throw new Exception();
        }
    }


    /**
     * 优惠券总数量减一
     * @param discountsId
     * @param discountsAmount
     * @return int
     */
    @Override
    public int upCouponNum(int discountsId, int discountsAmount) {
        int num = couponMapper.upCouponNum(discountsId, discountsAmount);
        return num;
    }


    /**
     * 发放优惠券
     * @param userId
     * @param discountsId
     * @return int
     */
    @Override
    public int addUserCoupon(Integer userId, Integer discountsId) {
        return couponMapper.addUserCoupon(userId, discountsId);
    }
}
